from django.shortcuts import render
from django.http import HttpResponse
from .models import Question,QuestionType,Choice,Answer
from random import sample
from .oo import produce_paper,ProducePaper

# Create your views here.


def index(request):#试卷选择
    
    context = {}
    return render(request,'paper/index.html',context)

def index_yunsuan(request):#生成试卷必要运算

    info = dict(request.POST)
    #context_dict = produce_paper(info)
    produce_paper_info = ProducePaper(info)
    context_dict = produce_paper_info.info_dict
    print(context_dict)
    context_dict_pk = produce_paper_info.info_dict_pk()
    
    print(context_dict_pk)
    
    context = {'context_dict':context_dict}
    return render(request,'paper/index_yunsuan.html',context)
    

def dati(request):#数据渲染
    
    context = {}
    return render(request,'paper/dati.html',context)

def dati_tijiao_info(request):#接收考试完提交的信息，并运算
    
    context = {}
    return render(request,'paper/dati_tijiao_info.html',context)

def info_jiekou(request):
    context = {}
    return render(request,'paper/info_jiekou.html',context)
	#return HttpResponse('提交成功')
	
def info_jiekou_yunsuan(request):

	'''<QueryDict: {'csrfmiddlewaretoken': 
	['Xu9GD5AqMDSwOub0pw7Ncjf3DfPB5yjcbcw2Dg
	o9MKAKc196rhH7lZfnWFTQ7R4z'], 
	'question_type': ['1'], 
	'question': ['所有M是P，所有S是M，
	因此不对的结论是（  ）'], 
	'choice': ['所有S是P', '所有S是Px', 
	'所有M是S', ''], 'answer': ['所有M是S']}>
	'''

	info_dict = dict(request.POST)
	type_id = info_dict['question_type'][0]
	if info_dict['question'][0] =='' or info_dict['answer'][0] == '':
	
		return HttpResponse('请检查是否输入问题和答案')
		
	
	question_type = QuestionType.objects.get(pk = type_id)
	
	question = question_type.question_set.create(
				question_text = info_dict['question'][0])
	
	question.answer_set.create(answer_text = info_dict['answer'][0])
	
	for choice in info_dict['choice']:
		if choice != '':
			question.choice_set.create(
				choice_text = choice)
	
	
	

	
	return HttpResponse('添加成功')
	
def random_default_paper(request):

    info_dict_default = {
        'question_type1':['1','10','1'],
        'question_type2':['3','5','2'],
        'question_type3':['6','5','2'],
        'question_type4':['5','10','3'],

        }
    produce_paper = ProducePaper(info_dict_default)
    context_dict =produce_paper.info_dict
    context_dict_pk = produce_paper.info_dict_pk()
    
    context = {'context_dict':context_dict,
			   'context_dict_pk':context_dict_pk,
	
	}

    return render(
                    request,
                    'paper/random_default_paper.html',
                    context,
                  )
















    

